<?php
/*
Plugin Name: JayBo Flickr User Icon
Plugin URI: http://www.jaybowalkin.com/plugins
Description: Grabs the latest thumbnail image (75px x 75px) from a Flickr feed and uses it as your user icon, this feed could be created using tags or other queries
Author: Bryan Clark
Version: 0.2
Author URI: http://www.jaybowalkin.com/

*/ 

# the JayBo default feed url, http://www.flickr.com/photos/jaybowalkin/tags/jaybo/
$d_rss_icon_feed_url = "http://api.flickr.com/services/feeds/photos_public.gne?id=8882003@N04&tags=jaybo&lang=en-us&format=rss_200";

function jaybo_edit_user_profile( ) {
  global $user_id, $profileuser;
?>
  <fieldset>
  <legend>Flickr Feed URL for Icons</legend>
  <p>Look to the bottom of any flick page for the latest Feed link, copy and past that link into here.  By default all your author links will be diverted to pictures of <a href="http://www.flickr.com/photos/jaybowalkin/tags/jaybo/">JayBo</a>.</p>
  <p><label>Flickr RSS Feed: (ex. http://api.flickr.com/feeds/photos_public.gne?... )<br />
  <input type="text" name="rss_icon_feed_url" value="<?php echo $profileuser->rss_icon_feed_url ?>"/>
  </label></p>
  </fieldset>

<?php
}
add_action( 'edit_user_profile', jaybo_edit_user_profile );

function jaybo_profile_update($user_id) {
  global $d_rss_icon_feed_url;

  $rss_icon_feed_url = $_POST['rss_icon_feed_url'];

	if ( empty($rss_icon_feed_url ) ) {
    $rss_icon_feed_url = $d_rss_icon_feed_url;
  }

  if ( strpos( $rss_icon_feed_url, "http://api.flickr.com/services/feeds/photos_public.gne") !== 0 ) {
    $rss_icon_feed_url = $d_rss_icon_feed_url;
  }

 	update_usermeta( $user_id, 'rss_icon_feed_url', $rss_icon_feed_url );

}

add_action( 'profile_update', jaybo_profile_update );

function jaybo_profile_get_user_icon( $user_ID ) {
  global $wpdb, $d_rss_icon_feed_url;

  $rss_icon_feed_url = get_usermeta( $user_ID, 'rss_icon_feed_url' );

  if( file_exists( ABSPATH . WPINC . '/rss.php' ) ) {
    require_once( ABSPATH . WPINC . '/rss.php' );
  } else {
    require_once( ABSPATH . WPINC . '/rss-functions.php');
  }

  if ( empty($rss_icon_feed_url) ) {
    $rss_icon_feed_url = $d_rss_icon_feed_url;
   	update_usermeta( $user_ID, 'rss_icon_feed_url', $rss_icon_feed_url );
  }

 	$usermeta_rss_icon_thumb_src = get_usermeta($user_ID, 'rss_icon_thumb_src');

  $rss_icon_thumb_src = $rss_icon_med_src = $rss_icon_href = $rss_icon_title = "";

  $rss = fetch_rss( $rss_icon_feed_url );

  if( is_array( $rss->items ) ) {
    foreach ($rss->items as $key => $photo) {
      preg_match_all("/<IMG.+?SRC=[\"']([^\"']+)/si",$photo[ 'description' ],$sub,PREG_SET_ORDER);
      $rss_icon_med_src = $sub[0][1];
      $rss_icon_thumb_src = str_replace( "_m.jpg", "_s.jpg", $sub[0][1] );
      $rss_icon_href = $photo[ 'link' ];
      $rss_icon_title = wp_specialchars( $photo[ 'title' ], true );
      if (!empty($rss_icon_thumb_src)) break;
    }

  }

  if ( $rss_icon_thumb_src != $usermeta_rss_icon_thumb_src ) {
   	update_usermeta($user_ID, 'rss_icon_med_src', $rss_icon_med_src);
   	update_usermeta($user_ID, 'rss_icon_thumb_src', $rss_icon_thumb_src);
   	update_usermeta($user_ID, 'rss_icon_href', $rss_icon_href);
   	update_usermeta($user_ID, 'rss_icon_title', $rss_icon_title);
  }

  return $rss_icon_thumb_src;
}

?>
